#region System
using System;
using System.Collections.Generic;
using System.IO;
using CF.Framework.Engine.Drawing;
using CF.Framework.Engine.Misc;
using CF.Framework.Engine.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace CF.Framework.Engine.Effects
{
	public abstract class FadeEffect : Effect
	{
		private int alphaValue;
		private double step;
		private bool plus;

		public int From { get; set; }
		public int To { get; set; }
		public TimeSpan Duration { get; set; }

		public FadeEffect()
		{
			From = 1;
			To = 255;
			Duration = TimeSpan.FromSeconds(2);
		}

		public override void OnStart()
		{
			step = Math.Abs(From - To) / (Duration.TotalSeconds * GameApp.Current.InitialFps);

			if (From > To)
			{
				step = 0 - step;
				plus = false;
			}
			else
			{
				plus = true;
			}

			alphaValue = From;
		}

		public override void OnStop()
		{
		}

		public override void OnUpdate(GameTime gameTime)
		{
			if (!IsRunning)
			{
				return;
			}

			if (IsDoing
				&& Target != null)
			{
				alphaValue += (int)step;

				if ((plus && alphaValue >= To)
					|| (!plus && alphaValue < To))
				{
					alphaValue = To;

					IsCompleted = true;
					IsDoing = false;
					IsRunning = false;
				}

				Target.Opacity = alphaValue;
				Target.Visibility = Controls.Visibility.Visible;
			}
		}
	}
}
